import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:excel_utils/pages/daily/daily_report/bean/CustomerBean.dart';
import 'package:excel_utils/pages/week/bean/MonthlyPackageTransferBean.dart';
import 'package:excel_utils/repository/k8/k8_repository.dart';
import 'package:excel_utils/repository/week/monthly_package_transfer_repository.dart';

part 'monthly_package_transfer_state.dart';

class MonthlyPackageTransferCubit extends Cubit<MonthlyPackageTransferState> {
  MonthlyPackageTransferRepository monthlyPackageTransferRepository;
  K8Repository _k8Repository;
  MonthlyPackageTransferCubit(this.monthlyPackageTransferRepository, this._k8Repository) : super(MonthlyPackageTransferInitial());

  refresh() async {
    emit(MonthlyPackageTransferRefreshing(timestamp: DateTime.now().millisecondsSinceEpoch));
  }

  saveMonthlyPackageTransfer({required List<MonthlyPackageTransferBean> monthlyPackageTransferData}) async {
    emit(MonthlyPackageTransferSaveLoading());
    List<MonthlyPackageTransferBean> quotationRecordData =
        await monthlyPackageTransferRepository.saveMonthlyPackageTransferData(monthlyPackageTransferData: monthlyPackageTransferData);
    emit(MonthlyPackageTransferSaveSuccessState(monthlyPackageTransferData: quotationRecordData));
  }

  getMonthlyPackageTransfer({maps}) async {
    emit(MonthlyPackageTransferDataLoading());
    List<MonthlyPackageTransferBean> quotationRecordData = await monthlyPackageTransferRepository.getMonthlyPackageTransferData(maps: maps);
    emit(MonthlyPackageTransferDataSuccessState(monthlyPackageTransferData: quotationRecordData));
  }

  delMonthlyPackageTransfer({required String monthlyPackageTransferId}) async {
    emit(MonthlyPackageTransferDeleteLoading(monthlyPackageTransferId: monthlyPackageTransferId));
    String deleteQuotationRecordId =
        await monthlyPackageTransferRepository.deleteMonthlyPackageTransferData(monthlyPackageTransferId: monthlyPackageTransferId);
    if (deleteQuotationRecordId != "-1") {
      emit(MonthlyPackageTransferDeleteSuccessState(monthlyPackageTransferId: deleteQuotationRecordId));
    } else {
      emit(MonthlyPackageTransferDeleteFailureState(message: "删除失败", monthlyPackageTransferId: monthlyPackageTransferId));
    }
  }

  // 根据编号查询客户信息
  searchCustomer({required String? customerNo}) async {
    emit(MonthlyPackageTransferSearchCustomerLoading(customerNo: customerNo.toString()));
    // 调用接口查询客户信息
    CustomerBean customerBean = await _k8Repository.queryCustomerData(maps: {"cmCode": customerNo});
    print(customerBean.data);
    if (customerBean.status == 1 && customerBean.data != null) {
      emit(MonthlyPackageTransferSearchCustomerSuccessState(customerNo: customerNo.toString(), customerBean: customerBean));
    }
    // emit(MonthlyPackageTransferSearchCustomerSuccessState(customerNo:customerNo));
  }
}
